package com.atguigu.gmall.cart.api;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.product.SkuInfoFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;


@RestController
@RequestMapping("api/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @DeleteMapping("/deleteSku")
    public Result deleteSkuFromCart(@RequestBody List<String> skuIds, HttpServletRequest request) {

        String key = cartService.determinCartKey(request);
        if (!CollectionUtils.isEmpty(skuIds)) {
            cartService.ajaxDeleteCartItemBatch(key,skuIds);
        }
        return Result.ok();
    }

    @DeleteMapping("/deleteCart/{skuId}")
    public Result ajaxDeleteCart(@PathVariable("skuId") Long skuId, HttpServletRequest request) {
        String key = cartService.determinCartKey(request);
        cartService.ajaxDeleteCart(key, skuId);
        return Result.ok();
    }

    /**
     * 选中购物车
     *
     * @param skuId
     * @param checked
     * @param request
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result ajaxCheckCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("checked") Integer checked,
                                HttpServletRequest request) {
        String key = cartService.determinCartKey(request);
        cartService.ajaxCheckCart(key, skuId, checked);
        return Result.ok();
    }

    /**
     * 加减购物车数量
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result ajaxAddToCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("skuNum") Integer skuNum,
                                HttpServletRequest request) {
        //ajax请求过网关,过滤器会给请求头上加token
        String key = cartService.determinCartKey(request);
        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);

        return Result.ok(cartInfo);

    }


    /**
     * 最终表示把哪个商品添加到购物车
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart")
    public CartInfo addToCart(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              HttpServletRequest request) {

        /**
         * 指明购物车属于那个用户
         */
        //操作redis的完整key
        String key = cartService.determinCartKey(request);

        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);

        return cartInfo;
    }

    /**
     * 返回购物车中所有商品
     *
     * @return
     */
    @GetMapping("/cartList")
    public Result listCart(HttpServletRequest request) {
        String tempCartKey = cartService.tempCartKey(request);
        String userCartKey = cartService.userCartKey(request);

        //查询所有购物车数据
        List<CartInfo> cartInfos = cartService.listCart(tempCartKey, userCartKey);
        return Result.ok(cartInfos);
    }

    /**
     * 获取购物车中已经选中的商品
     */
    @GetMapping("/cartListChecked")
    public List<CartInfo> cartListChecked(HttpServletRequest request) {
        return cartService.getListCheckCartInfo(request);
    }
}
